package tmall.common.aspect;

import cn.dev33.satoken.stp.StpUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zhangservlet
 * @description: 全局鉴权切面
 * @date 2021/7/9
 */
@Component
@Aspect
public class AuthenticationAspect {
    //引入日志配置
    protected final Logger LOG = LogManager.getLogger(LogAspect.class);


    @Pointcut(value = "@annotation(tmall.common.util.IsLogin)")
    public void isLoginPointCut() {
    }

    @Pointcut(value = "@annotation(tmall.common.util.IsAdmin)")
    public void isAdminPointCut(){
    }

    @Around("isLoginPointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        StpUtil.checkLogin();
        return joinPoint.proceed();
    }

    @Around("isAdminPointCut()")
    public Object isAdmin(ProceedingJoinPoint joinPoint) throws Throwable {
        StpUtil.checkLogin();
        // 当前账号是否含有指定角色标识, 如果验证未通过，则抛出异常: NotRoleException
        StpUtil.checkRole("admin");
        return joinPoint.proceed();
    }
}
